package JAVACORE_TRAINING.ADVANCED.SWING.DropDrag;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

public class SumAB extends JFrame {

	private JPanel contentPane;
	/**
	 * @wbp.nonvisual location=621,-31
	 */
	private final JLabel label = new JLabel("New label");
	private JTextField txtNumberA;
	private JTextField txtNumberB;
	private JTextField txtResult;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SumAB frame = new SumAB();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SumAB() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblNumberA = new JLabel("Number A");

		txtNumberA = new JTextField();
		txtNumberA.setColumns(10);

		JLabel lblNumberB = new JLabel("Number B");

		txtNumberB = new JTextField();
		txtNumberB.setColumns(10);

		JLabel lblResult = new JLabel("Result");

		txtResult = new JTextField();
		txtResult.setColumns(10);

		JButton btnSumAB = new JButton("SumAB");
		btnSumAB.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtResult.setText("");
				String numA = txtNumberA.getText();
				String numB = txtNumberB.getText();
				int num1 = 0;
				int num2 = 0;
				try {
					num1 = Integer.parseInt(numA.trim());
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(null, "Wrong format: " + ex.toString());
					txtNumberA.requestFocus();
					return;
				}
				try {
					num2 = Integer.parseInt(numB.trim());
				} catch (NumberFormatException ex) {
					JOptionPane.showMessageDialog(null, "Wrong format: " + ex.toString());
					txtNumberB.requestFocus();
					return;
				}
				int sumAB = (num1 + num2);
				txtResult.setText(new Integer(sumAB).toString());
				//txtResult.setText(sumAB + "");
			}
		});
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(
				Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup().addGroup(gl_contentPane
						.createParallelGroup(
								Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup().addGap(45)
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addComponent(lblNumberA, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE,
												GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblNumberB, Alignment.TRAILING)
										.addComponent(lblResult, Alignment.TRAILING))
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_contentPane.createSequentialGroup()
												.addPreferredGap(ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
												.addComponent(txtResult, GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup().addGap(34)
												.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
														.addComponent(txtNumberB, GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
														.addComponent(txtNumberA)))))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
								.addContainerGap(147, Short.MAX_VALUE).addComponent(btnSumAB)))
						.addGap(212)));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING).addGroup(gl_contentPane
				.createSequentialGroup().addGap(64)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNumberA).addComponent(
						txtNumberA, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblNumberB).addComponent(
						txtNumberB, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(18)
				.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE).addComponent(lblResult).addComponent(
						txtResult, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(18).addComponent(btnSumAB).addContainerGap(50, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
	}
}
